﻿// -----------------------------------------------------------------------
// <copyright file="AssemblyInfo.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
// -----------------------------------------------------------------------
#region Usings

using System;
using System.Reflection;

#endregion

namespace GateShift.Servers.GameServer.HelperClasses
{
    /// <summary>
    /// Contains methods for retrieving C# Assembly Information identifying the executable
    /// </summary>
    public class AssemblyInfo
    {
        private Assembly _assembly;

        #region Class Specific
        /// <summary>
        /// Initializes a new instance of the AssemblyInfo class
        /// </summary>
        public AssemblyInfo()
        {
            _assembly = Assembly.GetEntryAssembly();
        }

        /// <summary>
        /// Initializes a new instance of the AssemblyInfo class
        /// </summary>
        /// <param name="Assembly">Assembly to pull information from</param>
        public AssemblyInfo(Assembly Assembly)
        {
            _assembly = Assembly;
        }
        #endregion

        /// <summary>
        /// Gets or sets the current assembly to pull information from
        /// </summary>
        public Assembly Assembly
        {
            get
            {
                return _assembly;
            }

            set
            {
                _assembly = value;
            }
        }

        /// <summary>
        /// Gets the title attribute from the assembly
        /// </summary>
        public string Title
        {
            get
            {
                object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                string returnString = string.Empty;

                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute attribute = (AssemblyTitleAttribute)attributes[0];

                    if (attribute.Title != string.Empty)
                    {
                        returnString = attribute.Title;
                    }
                }

                if (string.IsNullOrEmpty(returnString))
                {
                    returnString = System.IO.Path.GetFileNameWithoutExtension(_assembly.CodeBase);
                }

                return returnString;
            }
        }

        /// <summary>
        /// Gets the description attribute from the assembly
        /// </summary>
        public string Description
        {
            get
            {
                object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                string returnString = string.Empty;

                if (attributes.Length > 0)
                {
                    AssemblyDescriptionAttribute attribute = (AssemblyDescriptionAttribute)attributes[0];

                    if (attribute.Description != string.Empty)
                    {
                        returnString = attribute.Description;
                    }
                }

                return returnString;
            }
        }

        /// <summary>
        /// Gets the build configuration attribute from the assembly
        /// </summary>
        public string Configuration
        {
            get
            {
                object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false);
                string returnString = string.Empty;

                if (attributes.Length > 0)
                {
                    AssemblyConfigurationAttribute attribute = (AssemblyConfigurationAttribute)attributes[0];

                    if (attribute.Configuration != string.Empty)
                    {
                        returnString = attribute.Configuration;
                    }
                }

                return returnString;
            }
        }

        /// <summary>
        /// Gets the company attribute from the assembly
        /// </summary>
        public string Company
        {
            get
            {
                object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                string returnString = string.Empty;

                if (attributes.Length > 0)
                {
                    AssemblyCompanyAttribute attribute = (AssemblyCompanyAttribute)attributes[0];

                    if (attribute.Company != string.Empty)
                    {
                        returnString = attribute.Company;
                    }
                }

                return returnString;
            }
        }

        /// <summary>
        /// Gets the product attribute from the assembly
        /// </summary>
        public string Product
        {
            get
            {
                object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                string returnString = string.Empty;

                if (attributes.Length > 0)
                {
                    AssemblyProductAttribute attribute = (AssemblyProductAttribute)attributes[0];

                    if (attribute.Product != string.Empty)
                    {
                        returnString = attribute.Product;
                    }
                }

                return returnString;
            }
        }

        /// <summary>
        /// Gets the copyright attribute from the assembly
        /// </summary>
        public string Copyright
        {
            get
            {
                object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                string returnString = string.Empty;

                if (attributes.Length > 0)
                {
                    AssemblyCopyrightAttribute attribute = (AssemblyCopyrightAttribute)attributes[0];

                    if (attribute.Copyright != string.Empty)
                    {
                        returnString = attribute.Copyright;
                    }
                }

                return returnString;
            }
        }

        /// <summary>
        /// Gets the trademark attribute from the assembly
        /// </summary>
        public string Trademark
        {
            get
            {
                object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyTrademarkAttribute), false);
                string returnString = string.Empty;

                if (attributes.Length > 0)
                {
                    AssemblyTrademarkAttribute attribute = (AssemblyTrademarkAttribute)attributes[0];

                    if (attribute.Trademark != string.Empty)
                    {
                        returnString = attribute.Trademark;
                    }
                }

                return returnString;
            }
        }

        /// <summary>
        /// Gets the current UI culture name
        /// </summary>
        public string Culture
        {
            get
            {
                return System.Globalization.CultureInfo.CurrentUICulture.Name;
            }
        }

        /// <summary>
        /// Gets the current UI culture display friendly name
        /// </summary>
        public string CultureDisplay
        {
            get
            {
                return System.Globalization.CultureInfo.CurrentUICulture.DisplayName;
            }
        }

        /// <summary>
        /// Gets the assembly version
        /// </summary>
        public Version AssemblyVersion
        {
            get
            {
                return _assembly.GetName().Version;
            }
        }
    }
}